{% extends "admin/layout.html" %}

{% block title %}{{ project.name }} - 项目详情 - {{ super() }}{% endblock %}

{% block content %}
<div class="d-flex justify-content-between flex-wrap flex-md-nowrap align-items-center pt-3 pb-2 mb-3 border-bottom">
    <h1 class="h2">项目详情: {{ project.name }}</h1>
    <div class="btn-toolbar mb-2 mb-md-0">
        {% if (project.status == 'draft' and project.owner_id == current_user.id) or current_user.is_admin %}
        <a href="{{ url_for('jira.edit_project', project_id=project.id) }}" class="btn btn-sm btn-outline-primary me-2">
            <i class="fas fa-edit"></i> 编辑
        </a>
        {% endif %}

        {% if project.status == 'draft' and project.owner_id == current_user.id %}
        <form method="post" action="{{ url_for('jira.submit_project', project_id=project.id) }}" class="d-inline me-2">
            <button type="submit" class="btn btn-sm btn-outline-success" onclick="return confirm('确定提交审核吗？')">
                <i class="fas fa-paper-plane"></i> 提交审核
            </button>
        </form>
        {% endif %}

        {% if project.status == 'pending_review' and current_user.is_admin %}
        <button type="button" class="btn btn-sm btn-outline-primary me-2" data-bs-toggle="modal" data-bs-target="#reviewModal">
            <i class="fas fa-check-circle"></i> 审核项目
        </button>
        {% endif %}

        {% if project.status == 'approved' and project.owner_id == current_user.id %}
        <form method="post" action="{{ url_for('jira.start_project', project_id=project.id) }}" class="d-inline me-2">
            <button type="submit" class="btn btn-sm btn-outline-success" onclick="return confirm('确定开始项目吗？')">
                <i class="fas fa-play"></i> 开始项目
            </button>
        </form>
        {% endif %}

        {% if project.status == 'in_progress' and project.owner_id == current_user.id %}
        <form method="post" action="{{ url_for('jira.complete_project', project_id=project.id) }}" class="d-inline me-2">
            <button type="submit" class="btn btn-sm btn-outline-success" onclick="return confirm('确定完成项目吗？')">
                <i class="fas fa-flag-checkered"></i> 完成项目
            </button>
        </form>
        {% endif %}

        {% if project.status == 'completed' and project.reviewer_id == current_user.id %}
        <button type="button" class="btn btn-sm btn-outline-primary me-2" data-bs-toggle="modal" data-bs-target="#finalReviewModal">
            <i class="fas fa-star"></i> 结项评价
        </button>
        {% endif %}

        {% if current_user.is_admin %}
        <form method="post" action="{{ url_for('jira.delete_project', project_id=project.id) }}" class="d-inline me-2">
            <button type="submit" class="btn btn-sm btn-outline-danger" onclick="return confirm('确定删除项目吗？')">
                <i class="fas fa-trash"></i> 删除
            </button>
        </form>
        {% endif %}

        <a href="{{ url_for('jira.project_list') }}" class="btn btn-sm btn-outline-secondary">
            <i class="fas fa-arrow-left"></i> 返回列表
        </a>
    </div>
</div>

<div class="row">
    <!-- 项目基本信息 -->
    <div class="col-md-6">
        <div class="card shadow-sm mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">基本信息</h5>
            </div>
            <div class="card-body">
                <table class="table table-borderless">
                    <tr>
                        <th width="120">项目名称:</th>
                        <td>{{ project.name }}</td>
                    </tr>
                    <tr>
                        <th>描述:</th>
                        <td>{{ project.description or '无' }}</td>
                    </tr>
                    <tr>
                        <th>类别:</th>
                        <td>{{ project.category.name if project.category }}</td>
                    </tr>
                    <tr>
                        <th>级别:</th>
                        <td>
                            <span class="badge" style="background-color: {{ project.level.color if project.level else '#6c757d' }}">
                                {{ project.level.name if project.level }}
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <th>负责人:</th>
                        <td>{{ project.owner.name }}</td>
                    </tr>
                    <tr>
                        <th>状态:</th>
                        <td>
                            <span class="badge bg-{{
                                'secondary' if project.status == 'draft'
                                else 'warning' if project.status == 'pending_review'
                                else 'success' if project.status == 'approved'
                                else 'danger' if project.status == 'rejected'
                                else 'primary' if project.status == 'in_progress'
                                else 'info' if project.status == 'completed'
                                else 'dark'
                            }}">
                                {{ project.status }}
                            </span>
                        </td>
                    </tr>
                    <tr>
                        <th>进度:</th>
                        <td>
                            <div class="progress" style="height: 10px;">
                                <div class="progress-bar" role="progressbar" style="width: {{ project.progress }}%;"
                                     aria-valuenow="{{ project.progress }}" aria-valuemin="0" aria-valuemax="100"></div>
                            </div>
                            <small class="text-muted">{{ project.progress }}%</small>
                        </td>
                    </tr>
                    <tr>
                        <th>创建时间:</th>
                        <td>{{ project.created_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    </tr>
                    {% if project.submitted_at %}
                    <tr>
                        <th>提交时间:</th>
                        <td>{{ project.submitted_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    </tr>
                    {% endif %}
                    {% if project.reviewed_at %}
                    <tr>
                        <th>审核时间:</th>
                        <td>{{ project.reviewed_at.strftime('%Y-%m-%d %H:%M') }}</td>
                    </tr>
                    {% endif %}
                </table>
            </div>
        </div>

        <!-- 项目成员 -->
        <div class="card shadow-sm mb-4">
            <div class="card-header d-flex justify-content-between align-items-center">
                <h5 class="card-title mb-0">项目成员</h5>
                <span class="badge bg-primary">{{ project.members|length }} 人</span>
            </div>
            <div class="card-body">
                <ul class="list-group list-group-flush">
                    {% for member in project.members %}
                    <li class="list-group-item d-flex justify-content-between align-items-center">
                        <div>
                            <strong>{{ member.member.name }}</strong>
                            <small class="text-muted d-block">{{ member.member.department.name if member.member.department }}</small>
                        </div>
                        {% if member.score is not none %}
                        <span class="badge bg-info rounded-pill">{{ member.score }}分</span>
                        {% endif %}
                    </li>
                    {% endfor %}
                </ul>
            </div>
        </div>
    </div>

    <!-- 项目阶段和任务 -->
    <div class="col-md-6">
        <div class="card shadow-sm mb-4">
            <div class="card-header">
                <h5 class="card-title mb-0">项目阶段</h5>
            </div>
            <div class="card-body">
                <div class="accordion" id="stagesAccordion">
                    {% for stage in project.stages|sort(attribute='order') %}
                    <div class="accordion-item">
                        <h2 class="accordion-header" id="stageHeading{{ stage.id }}">
                            <button class="accordion-button {% if not loop.first %}collapsed{% endif %}" type="button"
                                    data-bs-toggle="collapse" data-bs-target="#stageCollapse{{ stage.id }}"
                                    aria-expanded="{% if loop.first %}true{% else %}false{% endif %}"
                                    aria-controls="stageCollapse{{ stage.id }}">
                                <div class="d-flex w-100 justify-content-between align-items-center">
                                    <span>{{ stage.name }}</span>
                                    <span class="badge bg-{{
                                        'secondary' if stage.status == 'not_started'
                                        else 'primary' if stage.status == 'in_progress'
                                        else 'success' if stage.status == 'completed'
                                        else 'danger'
                                    }} me-2">{{ stage.status }}</span>
                                </div>
                            </button>
                        </h2>
                        <div id="stageCollapse{{ stage.id }}" class="accordion-collapse collapse {% if loop.first %}show{% endif %}"
                             aria-labelledby="stageHeading{{ stage.id }}" data-bs-parent="#stagesAccordion">
                            <div class="accordion-body">
                                <p>{{ stage.description or '无描述' }}</p>

                                {% if stage.tasks %}
                                <h6 class="mt-3">任务列表</h6>
                                <ul class="list-group list-group-flush">
                                    {% for task in stage.tasks %}
                                    <li class="list-group-item">
                                        <div class="d-flex justify-content-between align-items-start">
                                            <div>
                                                <strong>{{ task.name }}</strong>
                                                <div class="text-muted small">{{ task.description or '无描述' }}</div>
                                                {% if task.assignee %}
                                                <div class="mt-1">
                                                    <small>负责人: {{ task.assignee.name }}</small>
                                                </div>
                                                {% endif %}
                                                {% if task.due_date %}
                                                <div>
                                                    <small>截止日期: {{ task.due_date.strftime('%Y-%m-%d') }}</small>
                                                </div>
                                                {% endif %}
                                            </div>
                                            <div class="text-end">
                                                <span class="badge bg-{{
                                                    'secondary' if task.status == 'todo'
                                                    else 'primary' if task.status == 'in_progress'
                                                    else 'success' if task.status == 'completed'
                                                    else 'danger'
                                                }}">{{ task.status }}</span>

                                                {% if task.rating %}
                                                <div class="mt-1">
                                                    {% for i in range(1, 6) %}
                                                    <i class="fas fa-star {% if i <= task.rating %}text-warning{% else %}text-muted{% endif %}"></i>
                                                    {% endfor %}
                                                </div>
                                                {% endif %}
                                            </div>
                                        </div>

                                        <div class="mt-2">
                                            <!-- 完成任务按钮（仅任务未完成时显示） -->
                                            {% if task.status != 'completed' and task.assignee_id == current_user.id %}
                                            <form method="post" action="{{ url_for('jira.complete_task', task_id=task.id) }}" class="d-inline">
                                                <button type="submit" class="btn btn-sm btn-success">完成任务</button>
                                            </form>
                                            {% endif %}

                                            <!-- 分配任务按钮（关键修复：简化权限条件） -->
                                            {% if project.owner_id == current_user.id or current_user.is_admin %}
                                            <a href="{{ url_for('jira.assign_task_form', task_id=task.id) }}" class="btn btn-sm btn-primary">
                                                <i class="fas fa-user-plus"></i> 分配任务
                                            </a>
                                            {% endif %}
                                        </div>
                                    </li>
                                    {% endfor %}
                                </ul>
                                {% else %}
                                <!-- 无任务时的提示（避免阶段容器空） -->
                                <p class="text-muted">此阶段暂无任务，<a href="{{ url_for('jira.create_task', stage_id=stage.id) }}">点击创建任务</a></p>
                                {% endif %}
                            </div>
                        </div>
                    </div>
                    {% else %}
                    <!-- 无阶段时的提示 -->
                    <p class="text-muted p-3">暂无阶段信息，<a href="{{ url_for('jira.create_stage', project_id=project.id) }}">点击创建阶段</a></p>
                    {% endfor %}
                </div>
            </div>
        </div>
        <!-- 其他原有卡片保持不变 -->
    </div>
</div>

<!-- 审核模态框 -->
<!-- 结项评价模态框 -->
{% if project.status == 'completed' and project.reviewer_id == current_user.id %}
<div class="modal fade" id="finalReviewModal" tabindex="-1" aria-labelledby="finalReviewModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="finalReviewModalLabel">结项评价: {{ project.name }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post" action="{{ url_for('jira.review_closure', project_id=project.id) }}">
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="action" class="form-label">评价结果</label>
                        <select class="form-select" id="action" name="action" required>
                            <option value="">-- 选择评价结果 --</option>
                            <option value="approve">通过</option>
                            <option value="reject">拒绝</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="score" class="form-label">项目评分 (0-100)</label>
                        <input type="number" class="form-control" id="score" name="score" min="0" max="100" required>
                    </div>
                    <div class="mb-3">
                        <label for="notes" class="form-label">评价意见</label>
                        <textarea class="form-control" id="notes" name="notes" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">提交评价</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endif %}

{% if project.status == 'pending_review' and current_user.is_admin %}
<div class="modal fade" id="reviewModal" tabindex="-1" aria-labelledby="reviewModalLabel" aria-hidden="true">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <h5 class="modal-title" id="reviewModalLabel">审核项目: {{ project.name }}</h5>
                <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
            </div>
            <form method="post" action="{{ url_for('jira.review_project', project_id=project.id) }}">
                <div class="modal-body">
                    <div class="mb-3">
                        <label for="action" class="form-label">审核结果</label>
                        <select class="form-select" id="action" name="action" required>
                            <option value="">-- 选择审核结果 --</option>
                            <option value="approve">通过</option>
                            <option value="reject">拒绝</option>
                        </select>
                    </div>
                    <div class="mb-3">
                        <label for="review_notes" class="form-label">审核意见</label>
                        <textarea class="form-control" id="review_notes" name="review_notes" rows="3"></textarea>
                    </div>
                </div>
                <div class="modal-footer">
                    <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">取消</button>
                    <button type="submit" class="btn btn-primary">提交审核</button>
                </div>
            </form>
        </div>
    </div>
</div>
{% endif %}

<style>
.timeline {
    position: relative;
    padding-left: 2rem;
}

.timeline:before {
    content: '';
    position: absolute;
    left: 0;
    top: 0;
    bottom: 0;
    width: 2px;
    background-color: #e9ecef;
}

.timeline-item {
    position: relative;
    margin-bottom: 1.5rem;
}

.timeline-item:before {
    content: '';
    position: absolute;
    left: -2rem;
    top: 0.5rem;
    width: 12px;
    height: 12px;
    border-radius: 50%;
    background-color: #6c757d;
}

.timeline-success:before {
    background-color: #28a745;
}

.timeline-danger:before {
    background-color: #dc3545;
}

.timeline-content {
    padding: 0.5rem 1rem;
    background-color: #f8f9fa;
    border-radius: 0.25rem;
}

.timeline-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 0.5rem;
}
</style>
{% endblock %}